package com.jd.tank;

import com.jd.tank.abstractfactory.BaseTank;
import com.jd.tank.abstractfactory.TankUtil;


import java.awt.*;

/**
 * @program: MyTank
 * @description:
 * @author: 王术平
 * @create: 2021-01-01 11:49
 **/
public class Tank extends BaseTank {
    private static final int SPEED = 4;

    private TankFrame tf = null;

    public Tank(int x, int y, Direction dir, Group group, TankFrame tf, boolean moving) {
        super(x, y, true, new Rectangle(x, y, WIDTH, HEIGHT), dir, moving, group);
        this.tf = tf;
    }


    private void boundsCheck() {
        if (this.getX() < 2) this.setX(2);
        if (this.getY() < 28) this.setY(28);
        if (this.getX() > TankFrame.GAME_WIDTH - Tank.WIDTH - 2) this.setX(TankFrame.GAME_WIDTH - Tank.WIDTH - 2);
        if (this.getY() > TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2) this.setY(TankFrame.GAME_HEIGHT - Tank.HEIGHT - 2);
    }


    @Override
    public void die() {
        this.setLiving(false);
        int eX = this.getX() + Tank.WIDTH / 2 - Explode.WIDTH / 2;
        int eY = this.getY() + Tank.HEIGHT / 2 - Explode.HEIGHT / 2;
        this.tf.explodes.add(tf.bf.createExplode(eX, eY));
    }
    @Override
    public void fire() {
        int bX = this.getX() + Tank.WIDTH / 2 - Bullet.WIDTH / 2;
        int bY = this.getY() + Tank.HEIGHT / 2 - Bullet.HEIGHT / 2;
        tf.bullets.add(tf.bf.createBullet(bX, bY, this.getDir(), this.getGroup(), this.tf));
        if (this.getGroup() == Group.GOOD) new Thread(() -> new Audio("audio/tank_fire.wav").play()).start();
    }


    private void move() {
        if (!this.isLiving()) return;
        if (!this.isMoving()) return;
        switch (this.getDir()) {
            case LEFT:
                this.setX(this.getX() - SPEED);
                break;
            case UP:
                this.setY(this.getY() - SPEED);
                break;
            case RIGHT:
                this.setX(this.getX() + SPEED);
                break;
            case DOWN:
                this.setY(this.getY() + SPEED);
                break;
        }

        if (this.getGroup() == Group.BAD && TankUtil.random.nextInt(100) > 95)
            this.fire();

        if (this.getGroup() == Group.BAD && TankUtil.random.nextInt(100) > 95)
            randomDirection();

        boundsCheck();
        //update rect
        this.getRect().x=this.getX();
        this.getRect().y=this.getY();
    }


    @Override
    public void paint(Graphics g) {
        if (!isLiving()) {
            this.setMoving(false);
            return;
        }
        switch (this.getDir()) {
            case LEFT:
                g.drawImage(this.getGroup() == Group.GOOD ? ResourceMgr.goodTankL : ResourceMgr.badTankL, this.getX(), this.getY(), null);
                break;
            case UP:
                g.drawImage(this.getGroup() == Group.GOOD ? ResourceMgr.goodTankU : ResourceMgr.badTankU, this.getX(), this.getY(), null);
                break;
            case RIGHT:
                g.drawImage(this.getGroup() == Group.GOOD ? ResourceMgr.goodTankR : ResourceMgr.badTankR, this.getX(), this.getY(), null);
                break;
            case DOWN:
                g.drawImage(this.getGroup() == Group.GOOD ? ResourceMgr.goodTankD : ResourceMgr.badTankD, this.getX(), this.getY(), null);
                break;
        }
        move();
    }

    private void randomDirection() {
        this.setDir(Direction.values()[TankUtil.random.nextInt(Direction.values().length)]);
    }


}
